package com.amar.dtadmin.apidoc.model;

import com.amar.dtadmin.apidoc.action.DtAdminApiDocActionGroup;
import com.amar.dtadmin.apidoc.extensions.storages.DtAdminSettings;
import com.amar.dtadmin.apidoc.util.ParamItemConvertUtil;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Collections;
import java.util.List;

/**
 * @author xiaoshiqiang
 * @since 2025/2/8
 */
@Data
@NoArgsConstructor
public class ApiDocCreateReq {
    protected String api;

    protected String chargeuser;

    protected String datadescr = "";

    protected String name;

    protected String descr;

    protected String groupid;

    protected String grouplevel;

    protected String inputuser;

    protected String method = "POST";

    protected String projectid;

    protected String reqsample = "";

    protected String respsample = "";

    protected String status = "DEVELOP";

    protected List<ParamItem> requestparamreq;

    protected List<ParamItem> responseparamreq;

    protected List<DataSourceDetail> dataSourceDetail = Collections.singletonList(new DataSourceDetail());

    protected List<?> tags = Collections.emptyList();

    public ApiDocCreateReq(DtAdminSettings settings, DtAdminApiDocActionGroup.ApiInfo apiInfo) {
        this.api = apiInfo.getApi();
        this.chargeuser = settings.getFullName();
        this.inputuser = settings.getFullName();
        this.name = apiInfo.getName();
        this.descr = apiInfo.getName();
        this.groupid = apiInfo.getGroupId();
        this.grouplevel = apiInfo.getGroupLevel();
        this.projectid = settings.getProjectId();
        this.requestparamreq = ParamItemConvertUtil.convert(apiInfo.getInputClass(), "REQUEST");
        this.responseparamreq = ParamItemConvertUtil.convert(apiInfo.getOutputClass(), "RESPONSE");
    }

    @Data
    public static class ParamItem {
        private Integer sortno;
        private String name;
        private String chname;
        private String datatype;
        private String isrequired;
        private String descr = "";
        private List<ParamItem> nextnode;
        private Integer level;
        private String id;
        private String nodeid;
        private String paramtype;
        private String parentnodeid;
        private String requesttype = "BODY";
        private Boolean show = false;
        private String contenttype = "application/json";
    }

    @Data
    public static class DataSourceDetail {
        private String configId = "t2024120200163016";
        private String dbType = "mysql";
        private String dbUrl = "t2024120200163016";
        private String tableName = "";
    }
}
